package com.rad.statistics.android;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.rad.statistics.common.cmd.StatisticsCmd;

/**
 * 类说明： 存储模块，将统计信息存储到本地
 * 
 * @author kalui.huang
 * @date 2014-10-16
 * @version 1.0
 */
public class DataStorage {

	private static final String STATISTICS_FILE = "statistics";
	private static File sStatisticsFile;

	/**
	 * 存储cmd,如果本地包含数据，则取出和新cmd数据合并再存储
	 * 
	 * @param cmd
	 * @return 是否保存成功
	 */
	public static synchronized boolean saveCmd(Context context, StatisticsCmd cmd) {
		getFile(context);

		ObjectOutputStream oos = null;
		try {
			// 新旧cmd统计数据合并
			//mergeCmd(context, cmd);

			oos = new ObjectOutputStream(new FileOutputStream(sStatisticsFile));
			oos.writeObject(cmd);
			oos.flush();
			oos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 获取全部本地统计数据
	 * 
	 * @return 统计cmd
	 */
	public static synchronized StatisticsCmd getAllCmd(Context context) {
		getFile(context);

		StatisticsCmd statisticsCmd = null;
		try {
			if (sStatisticsFile.exists()) {
				ObjectInputStream ois = new ObjectInputStream(new FileInputStream(sStatisticsFile));
				statisticsCmd = (StatisticsCmd) ois.readObject();
				ois.close();
				sStatisticsFile.delete();
				return statisticsCmd;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取本地统计数据,如果本地数据大于99条则只取99条。
	 * 
	 * @return 统计cmd
	 */
	public static synchronized StatisticsCmd getLimitedCmd(Context context) {
		getFile(context);

		StatisticsCmd statisticsCmd = null;
		try {
			if (sStatisticsFile.exists()) {
				ObjectInputStream ois = new ObjectInputStream(new FileInputStream(sStatisticsFile));
				statisticsCmd = (StatisticsCmd) ois.readObject();
				ois.close();
				sStatisticsFile.delete();
				//如果统计事件大于99条，只取前面99条，剩余的保存在文件中
				if(statisticsCmd.getDatas() != null && statisticsCmd.getDatas().size() >=100){
					List<String> bakData = statisticsCmd.getDatas();
					List<String> subData1 = new ArrayList<String>();
					List<String> subData2 = new ArrayList<String>();
					for(int i = 0; i < bakData.size(); i++){
						if(i < 99){
							subData1.add(bakData.get(i));
						}else{
							subData2.add(bakData.get(i));
						}
					}
					statisticsCmd.setDatas(subData2);
					saveCmd(context, statisticsCmd);
					statisticsCmd.setDatas(subData1);
				}
				return statisticsCmd;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 合并cmd的统计数据
	 * 
	 * @param cmd
	 */
	public static void mergeCmd(Context context, StatisticsCmd cmd, StatisticsCmd mergeCmd) {
		getFile(context);

		//StatisticsCmd storedCmd = getCmd(context);
		if (mergeCmd != null) {
			List<String> storedList = mergeCmd.getDatas();
			cmd.addDatas(storedList);
		}
		
	}

	/**
	 * 获取保存统计数据的文件引用
	 */
	private static void getFile(Context context) {
		if (sStatisticsFile == null) {
			String path = context.getFilesDir().getAbsolutePath() + File.separator;
			sStatisticsFile = new File(path + STATISTICS_FILE);
		}
	}

}
